<?php
class Home extends CI_Controller {
	var $upload_dir='upload/';
    public function __construct()
    {
        parent::__construct();
        //$this->load->library('auth');
        $this->load->helper('global');
      //  $this->auth->user_logged_in();
    }
    
	public function autocomplete()
    {
		$q = strtolower($_GET["q"]);
		if (!$q) return;
		$query=$this->db->query('SELECT * FROM client WHERE client_name like "%'.$q.'%"');
		$client= $query->result_array();	
		//pr($client);
		foreach ($client as $key=>$value) {
			if (strpos(strtolower($value['client_name']), $q) !== false) {
				echo "".$value['client_name']."|".$value['id']."\n";
				//echo "".$value['client_name']."\n";
				
			}
		}
	}
	public function menu()
    {
		$data['main'] 	= 'front/menu';
		$data['page_title'] 	= 'Brown';
		$this->load->view('layout/ad_blank',$data);	
	}
	public function index()
      {
		$data['main'] 	= 'front/homepage';
		$data['page_title'] 	= 'Brown';
		$this->load->view('layout/ad_default',$data);
    }
	public function listclient()
    {
		$q=$this->db->query('SELECT * FROM client');
		
		$data['client'] 	= $q->result_array();	
		$data['main'] 	= 'front/listclient';
		$data['page_title'] 	= 'Brown';
		$this->load->view('layout/ad_blank',$data);
	}
	public function editclient($id)
    {
		if(isset($_POST['busines_sector'])){
			$this->db->where('id', $_POST['id']);
			unset($_POST['id']);
			$this->db->update('client',$_POST);
		}
		$q=$this->db->query('SELECT * FROM client WHERE id='.$id.'');
		$data['client'] 	= $q->result_array();
		$data['main'] 	= 'front/editclient';
		$data['page_title'] 	= 'Brown';
		$this->load->view('layout/ad_blank',$data);
    }
	public function editdetail($id_page=null)
    {
		$this->load->model('page');
		$currentpage=$this->page->get_current_pageById($id_page);
		$current=$this->page->getContentByIdPage($id_page);
		$currentfile=$this->page->getContentFileByIdContent(@$current[0]['id']);
		
		if(isset($_POST['submit'])){
			if(empty($current)){
				$this->db->insert('content', array('id_page'=>$id_page,'title'=>'','desc'=>''.$_POST['desc'].''));
				echo mysql_insert_id();			
			}else{
				$this->db->where('id',$id_page);
				$this->db->update('content',array('id_page'=>$id_page,'title'=>'','desc'=>''.$_POST['desc'].''));
				echo $id_page;
			}

			die();
		}
		
		$data['currentpage'] 	=$currentpage;
		$data['current'] 	=$current;
		$data['currentfile'] 	=$currentfile;
		$data['id_page'] 	=$id_page;
		$data['main'] 	= 'front/editdetail';
		$data['page_title'] 	= 'Brown';
		$this->load->view('layout/ad_blank',$data);
	}
	
    public function deletefile($id_file=null)
    {
		//pr($_POST);
		if($this->db->query('DELETE FROM file WHERE id="'.$_POST['id_file'].'"')){
			unlink($this->upload_dir.$_POST['file_name']);
		}
	}
    public function upload($id_content=null)
    {
			foreach ($_FILES["file_name"]["error"] as $key => $error) {
				if ($error == UPLOAD_ERR_OK) {
					$name = date('Ymdhis').str_replace(" ","",$_FILES["file_name"]["name"][$key]);
					if(move_uploaded_file( $_FILES["file_name"]["tmp_name"][$key], $this->upload_dir . $name)){
						$this->db->insert('file', array('id_content'=>$id_content,'file_name'=>''.$name.''));
					}
					
				}
			}
    }
	public function halaman()
    {
		$data['main'] 	= 'front/halaman';
		$data['page_title'] 	= 'Brown';
		$this->load->view('layout/ad_blank',$data);	
	}
	public function addpage($page=null,$id_client=null,$halamanklik='')
    {
		$this->load->model('page');
		if($page==null){
			$page=1;
		}
		if(isset($_POST['id_client'])){
		$id_client=$_POST['id_client'];
		}
		if(isset($_POST['title'])){
			$currentpage1=$this->page->get_current_pageById($_POST['id_page']);
			//pr($currentpage1);
			
			$insertpage=array(
							'id_client'=>$_POST['id_client'],
							'page_number'=>$_POST['page_number'],
							'title'=>$_POST['title']
			);
			if(empty($currentpage1)){
				$this->db->insert('page',$insertpage);
				echo mysql_insert_id();
			}else{
				$this->db->where('id',$_POST['id_page']);
				$this->db->update('page',$insertpage);
				echo $_POST['id_page'];
			}
			//echo $this->db->last_query();
			
			die();
		}
		
		
		$currentpage=$this->page->get_current_page($page,$id_client);
		//pr($currentpage);
		
		$data['halamanklik'] 	= $halamanklik;
		$data['currentpage'] 	= $currentpage;
		$data['id_client'] 	= $id_client;
		$data['page_number'] 	= $page;
		$data['page'] 	= $page;
		$data['main'] 	= 'front/addpage';
		$data['page_title'] 	= 'Brown';
		$this->load->view('layout/ad_blank',$data);	
	}
	public function addclient()
    {
		if(isset($_POST['busines_sector'])){
			$this->db->insert('client',$_POST);
		}
		$data['main'] 	= 'front/addclient';
		$data['page_title'] 	= 'Brown';
		$this->load->view('layout/ad_blank',$data);
    }
    
}
?>